package com.tjp.eight.views;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.tjp.eight.R;
import com.tjp.eight.views.emoj.Emoji;
import com.tjp.eight.views.emoj.EmojiUtil;
import com.tjp.eight.views.emoj.FaceFragment;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/2/22.
 */

public class SendCommentDialog extends Dialog implements FaceFragment.OnEmojiClickListener {
    private Context mContext;
    private LayoutInflater inflater;
    private WindowManager.LayoutParams lp;
    FrameLayout fl_emo_container;
    ImageView iv_emo;
    TextView tv_send_comment;

    private EditTextPreIme et_comment_content;

    public interface SendComment {
        void sendComment(String msg);
    }

    private SendComment sendComment;

    public void setSendComment(SendComment sendComment) {
        this.sendComment = sendComment;
    }

    public SendCommentDialog(final Context context) {
        super(context, R.style.Dialog);

        this.mContext = context;

        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.send_comment_layout, null);
        iv_emo = (ImageView) layout.findViewById(R.id.iv_emo_send);
        iv_emo.setVisibility(View.GONE);
        fl_emo_container = (FrameLayout) layout.findViewById(R.id.fl_emo_container);
        et_comment_content = (EditTextPreIme) layout.findViewById(R.id.et_comment_content);
        et_comment_content.setDismissSomething(new EditTextPreIme.DismissSomething() {
            @Override
            public void dismissT() {
                dismiss();
            }
        });
        tv_send_comment = (TextView) layout.findViewById(R.id.tv_send_comment);
        tv_send_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sendComment != null) {
                    sendComment.sendComment(et_comment_content.getText().toString());
                }
            }
        });
        et_comment_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fl_emo_container.setVisibility(View.GONE);
                isHide = true;
                setEmo();
            }
        });
        iv_emo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fl_emo_container.getVisibility() == View.GONE) {
                    setKey();
                    hideKeyboard();
                    (new Handler()).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            fl_emo_container.setVisibility(View.VISIBLE);
                        }
                    }, 200);
                    isHide = false;

                } else {
                    setEmo();
                    isHide = true;
                    fl_emo_container.setVisibility(View.GONE);
                    Timer timer = new Timer();
                    timer.schedule(new TimerTask() {

                                       public void run() {
                                           showKeyboard();
                                       }
                                   },
                            200);
                }
            }
        });
        if (context instanceof FragmentActivity) {
            FaceFragment faceFragment = (FaceFragment) ((FragmentActivity) context).getSupportFragmentManager().findFragmentById(R.id.f_emo);
            faceFragment.setListener(this);
        }
        try {
            Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
            field.setAccessible(true);
            field.set(et_comment_content, R.drawable.cursor_style);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


        setContentView(layout);

        // 设置window属性
        lp = getWindow().getAttributes();
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        lp.width = point.x;
        lp.gravity = Gravity.BOTTOM;
        lp.alpha = 1.0f;
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);
    }

    boolean isHide = true;

    public boolean isHide() {
        return isHide;
    }

    @Override
    public void show() {
        super.show();
        et_comment_content.requestFocus();
        et_comment_content.setFocusableInTouchMode(true);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                showKeyboard();
            }
        }, 200);

    }

    @Override
    public void dismiss() {
        super.dismiss();
        hideKeyboard();
        fl_emo_container.setVisibility(View.GONE);
        isHide = true;
        setEmo();
    }

    void setEmo() {
        Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.emoji);
        iv_emo.setImageBitmap(bitmap);
    }

    void setKey() {
        Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.keyboard);
        iv_emo.setImageBitmap(bitmap);
    }


    private void showKeyboard() {
        InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(et_comment_content, 0);
    }

    private void hideKeyboard() {
        InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }


    @Override
    public void onEmojiDelete() {
        int action = KeyEvent.ACTION_DOWN;
        int code = KeyEvent.KEYCODE_DEL;
        KeyEvent event = new KeyEvent(action, code);
        et_comment_content.onKeyDown(KeyEvent.KEYCODE_DEL, event);
    }

    @Override
    public void onEmojiClick(Emoji emoji) {
        try {
            new EmojiUtil().handlerEmojiText(et_comment_content, emoji.getContent(), getContext());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setEditText(String msg) {
        et_comment_content.setText(msg);
    }

}
